from PyQt5.QtWidgets import *
from myutil import globalvar as gl, logs
from myutil.api.jwc_data_process import yxkc_list_element_to_dict
from ui.now_select_course import Ui_Frame as now_select_course
from PyQt5.QtGui import QStandardItemModel, QStandardItem


class now_select_course_form(QFrame, now_select_course):
    def __init__(self, parent=None):
        super(now_select_course_form, self).__init__(parent)
        self.setupUi(self)

        self.fresh_yxkc_tableView()

    def fresh_yxkc_tableView(self):
        logs.logs_print("fresh_当前已选课程")

        if gl.get_value("yxkc_list") is None:
            self.label.setText("请先登录，再查看")
            return 0

        self.yxkc_list = gl.get_value("yxkc_list")
        res = []
        for i in self.yxkc_list:
            # res.append({"info": i})
            res.append(yxkc_list_element_to_dict(i))
            # res.append(course_list_element_to_dict(i))

        if len(res) == 0:
            logs.logs_print("结果为空")
            model = QStandardItemModel(1, 1)
            model.setHorizontalHeaderLabels(["结果"])
            model.setItem(0, 0, QStandardItem("结果为空"))
            self.yxkc_tableView.setModel(model)
            return

        columns = res[0].keys()
        # 设置数据层次结构，4行4列
        model = QStandardItemModel(len(res), len(columns))
        # 设置水平方向四个头标签文本内容
        model.setHorizontalHeaderLabels(columns)

        for i in range(len(res)):
            for column, data in enumerate(res[i].values()):
                item = QStandardItem(str(data))
                # 设置每个位置的文本值
                model.setItem(i, column, item)

        # 实例化表格视图，设置模型为自定义的模型
        self.yxkc_tableView.setModel(model)
